package com.web.biz.grgn.service;
import java.util.Map;

import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.kernel.utils.SwordSecurityUtils;
import com.css.sword.kernel.utils.SwordServiceUtils;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;
import com.web.common.dataDictionary.DE.DE_ID;
import com.web.common.dataDictionary.PO.TabUserPO;

@ServiceContainer
public class ResetPwdService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(ResetPwdService.class);
	static private final String path = "pages/qt/sygn/zhmm/";
	public ResetPwdService() throws Exception {
		super();
	}


	/**
	 * init
	 */
	@Service(serviceName = "ResetPwdService_init")
	public IResData init(IReqData req) throws Exception {
		log.debug("开始进入 ResetPwdService_init 方法。");
		//List list = PullUtil.getData("TAB_CODE_MMTSWT");
		IResData res = new SwordRes();
		//res.addListWithDataName("question", list);
		res.addPage(path+"resetPassword.jsp");
		return res;
	}

	 		
	/**
	 * 根据用户名查询信息
	 */
	@Service(serviceName = "ResetPwdService_validateByUsername")
	public IResData validateByUsername(IReqData req) throws Exception {

		log.debug("开始进入 ResetPwdService_validateByUsername 方法。");
		String msg = "true";
		String tsmmwt = "";
		Map<String, String> userMap = req.getForm("form_resetpwd");
		if (userMap!=null) {
			String username = userMap.get("username");
			Map<String, Object> resMap = (Map<String, Object>) SwordServiceUtils.callLocalService("LoginRegisterService_queryByYhm", username);
			if (resMap!=null) {
				tsmmwt = (String) resMap.get("tsmmwt");
			}else {
				msg = "用户名不存在";
			}
		}
		IResData res = new SwordRes();
		res.addAttr("msg", msg);
		res.addAttr("tsmmwt", tsmmwt);
		return res;

	}

	 		
	/**
	 * 验证找回答案
	 */
	@Service(serviceName = "ResetPwdService_validateTsmmda")
	public IResData validateTsmmda(IReqData req) throws Exception {

		log.debug("开始进入 ResetPwdService_validateTsmmda 方法。");

		String msg = "回答问题不正确";
		Map<String, String> userMap = req.getForm("form_resetpwd");
		String qttsmmda = userMap.get("tsmmda");
		if (userMap!=null) {
			String username = userMap.get("username");
			Map<String, Object> resMap = (Map<String, Object>) SwordServiceUtils.callLocalService("LoginRegisterService_queryByYhm", username);
			if (resMap!=null) {
				Object tsmmda = resMap.get("tsmmda");
				if (tsmmda!=null&&tsmmda.equals(qttsmmda)) {
					msg ="true"; 
				}
			}
		}

		IResData res = new SwordRes();
		res.addAttr("msg", msg);
		return res;

	}

	 		
	/**
	 * 验证证件号码
	 */
	@Service(serviceName = "ResetPwdService_validateZjbh")
	public IResData validateZjbh(IReqData req) throws Exception {

		log.debug("开始进入 ResetPwdService_validateZjbh 方法。");
		String msg = "证件号码不正确";
		Map<String, String> userMap = req.getForm("form_resetpwd");
		String qtzjbh = userMap.get("zjbh");
		String id = "";
		if (userMap!=null) {
			String username = userMap.get("username");
			Map<String, Object> resMap = (Map<String, Object>) SwordServiceUtils.callLocalService("LoginRegisterService_queryByYhm", username);
			if (resMap!=null) {
				Object zjbh = resMap.get("zjbh");
				if (zjbh!=null&&zjbh.equals(qtzjbh)) {
					msg ="true"; 
					id = (String) resMap.get("id");
				}
			}
		}
		IResData res = new SwordRes();
		res.addAttr("msg", msg);
		res.addAttr("id", id);
		return res;
	}

	 		
	/**
	 * 重置密码
	 */
	@Service(serviceName = "ResetPwdService_resetPassword")
	public IResData resetPassword(IReqData req) throws Exception {

		log.debug("开始进入 ResetPwdService_resetPassword 方法。");
		Map<String, String> userMap = req.getForm("form_resetpwd");
		String id = userMap.get("id");
		String password = userMap.get("password");
		String pwdMD5 = SwordSecurityUtils.md5(password.getBytes());
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		TabUserPO po = new TabUserPO();
		po.setId(new DE_ID(id));
		//po.setUserpassword(new DE_USERPASSWORD(pwdMD5));
		dao.updatePO(po);
		IResData res = new SwordRes();
		res.addMessage("密码重置成功");
		return res;

	}

	 		


}